package com.dl.exam.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dl.exam.entity.User;
import com.dl.exam.mapper.UserMapper;
import com.dl.exam.service.CommonService;
import com.dl.exam.util.UUIDUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author DL
 * @create 2022-01-17 14:40
 */
@Service
@Transactional
public class CommonServiceImpl implements CommonService {

    @Resource
    private UserMapper userMapper;

    @Override
    public User login(String username, String password) {
        return userMapper.login(username, password);
    }

    @Override
    public boolean register(String username, String password) {
        String uuid10 = UUIDUtils.getUUID10();
        return userMapper.addUser(uuid10, username, password) != 0;
    }

    @Override
    public boolean modifyPassword(String newPassword, String user_id, String password) {
        return userMapper.updatePassword(newPassword, user_id, password) == 1;
    }

    @Override
    public boolean modifyUserBasicInfo(String userId, JSONObject jsonObject) {
        String name = jsonObject.getString("name");
        String email = jsonObject.getString("email");
        String phone = jsonObject.getString("phone");
        return userMapper.updateUserBasicInfo(userId, name, email, phone) != 0;
    }

    @Override
    public User getUserBasicInfo(String userId) {
        return userMapper.queryUserBasicInfo(userId);
    }

    @Override
    public boolean checkUsername(String username) {
        return userMapper.queryUserByUsername(username) == null;
    }

    @Override
    public Map<String, String> getRoleById(String userId) {
        return userMapper.queryRoleById(userId);
    }

}
